package cn.itcast.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.itcast.dao.MemberProfileDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberProfileService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@Service
public class MemberProfileServiceImpl implements MemberProfileService {
    @Autowired
    private MemberProfileDao memberProfileDao;

    /**
     * 根据条件查询会员
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize,Integer total,String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberProfileDao.selectAll(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加会员信息
     * @param member
     * @return
     */
    @Override
    public int addMember(Member member) {
        if (StringUtils.isEmpty(member.getName())||StringUtils.isEmpty(member.getPhoneNumber())||StringUtils.isEmpty(member.getIdCard())){
            return 0;
        }
        try {
            member.setRegTime(DateUtils.parseString2Date(DateUtil.today()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        member.setStatus(0);
        memberProfileDao.addMember(member);
        return 1;
    }

    /**
     * 修改会员信息
     * @param member
     * @return
     */
    @Override
    public int updateById(Member member) {
        return memberProfileDao.updateById(member);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Member selectById(Integer id) {
        return memberProfileDao.selectById(id);
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        return memberProfileDao.updateStatus(id,status);
    }
}